using System;
using System.Windows.Forms;

namespace ClientTools.Client
{
    public partial class FrmMain : Form
    {
        public FrmMain()
        {
            InitializeComponent();
        }

        private UserControl actControl;
        private Library.Settings.Profile actProfile;
        private bool bSetData;
        private Controls.ControlInfoCollection controlInfos;
        //private Client.Controls.ControlInfoCollection _ControlInfosNoProfile = null;

        private void FrmMainLoad(object sender, EventArgs e)
        {
            Text += Application.ProductVersion.Substring(0, Application.ProductVersion.LastIndexOf("."));
            bSetData = true;
            FillProfiles();
            bSetData = false;
            cboProfiles.SelectedIndex = 0;

            controlInfos = Client.Controls.ControlsManager.GetAllUserControls(Application.StartupPath);
            //_ControlInfosNoProfile = Client.Controls.ControlsManager.GetAllUserControlsNoProfile(Application.StartupPath);
            FillButtons();
        }

        private void FillButtons()
        {
            var addIndex = 0;

            for (var i = 0; i < controlInfos.Count; i++)
            {
                var ci = controlInfos[i];

                if (ci.ShowDelimiter && addIndex > 0)
                {
                    var tss = new ToolStripSeparator();
                    ToolStrip1.Items.Insert(addIndex++, tss);
                }

                var ti = ToolStrip1.Items.Add(ci.ButtonText);
                ToolStrip1.Items.Insert(addIndex++, ti);
                ti.Tag = ci;
                ti.Image = ci.ButtonImage;
                ti.ImageTransparentColor = System.Drawing.Color.Magenta;
                ti.Name = "tsb" + ci.TypeName;
                ti.Padding = new Padding(5);
                ti.Size = new System.Drawing.Size(68, 43);
                ti.Text = ci.ButtonText;
                ti.TextImageRelation = TextImageRelation.ImageAboveText;
                ti.Click += TsbUserControlClick;
            }
            //if (addIndex > 0)
            //{
            //    ToolStripSeparator tss = new ToolStripSeparator();
            //    this.ToolStrip1.Items.Insert(addIndex++, tss);
            //    tss = new ToolStripSeparator();
            //    this.ToolStrip1.Items.Insert(addIndex++, tss);
            //}
            //for (int i = 0; i < _ControlInfosNoProfile.Count; i++)
            //{
            //    Client.Controls.ControlInfo ci = _ControlInfosNoProfile[i];

            //    if (ci.ShowDelimiter && i > 0)
            //    {
            //        ToolStripSeparator tss = new ToolStripSeparator();
            //        this.ToolStrip1.Items.Insert(addIndex++, tss);
            //    }

            //    ToolStripItem ti = ToolStrip1.Items.Add(ci.ButtonText);
            //    this.ToolStrip1.Items.Insert(addIndex++, ti);
            //    ti.Tag = ci;
            //    ti.Image = ci.ButtonImage;
            //    ti.ImageTransparentColor = System.Drawing.Color.Magenta;
            //    ti.Name = "tsb" + ci.TypeName;
            //    ti.Padding = new System.Windows.Forms.Padding(5);
            //    ti.Size = new System.Drawing.Size(68, 43);
            //    ti.Text = ci.ButtonText;
            //    ti.TextImageRelation = System.Windows.Forms.TextImageRelation.ImageAboveText;
            //    ti.Click += new System.EventHandler(this.TsbUserControlClick);
            //}
        }

        private void FillProfiles()
        {
            cboProfiles.Items.Clear();
            foreach (Library.Settings.Profile var in Globals.AppSettings.Profiles)
                cboProfiles.Items.Add(var.ProfileName);
        }

        private void ShowUserControl(UserControl uc)
        {
            Cursor = Cursors.WaitCursor;
            var t = uc.GetType();

            if (actControl != null)
            {
                if (actControl.GetType() == typeof(Controls.Settings.UcSettings))
                {
                    ((Controls.Settings.UcSettings)actControl).ProfileSavedEvent -= ProfileSaved;
                    ((Controls.Settings.UcSettings)actControl).ProfileDeletedEvent -= ProfileDeleted;
                    ((Controls.Settings.UcSettings)actControl).NewProfileAddedEvent -= NewProfileAdded;
                }
                actControl.Hide();
                pnlMain.Controls.Clear();
                actControl = null;
            }
            actControl = uc;
            pnlMain.Controls.Add(actControl);
            actControl.Dock = DockStyle.Fill;

            if (t == typeof(Controls.Settings.UcSettings))
            {
                ((Controls.Settings.UcSettings)actControl).ProfileSavedEvent += ProfileSaved;
                ((Controls.Settings.UcSettings)actControl).ProfileDeletedEvent += ProfileDeleted;
                ((Controls.Settings.UcSettings)actControl).NewProfileAddedEvent += NewProfileAdded;
            }

            if (t.GetInterface(typeof(Controls.IUserControl).Name) != null)
            {
                ((Controls.IUserControl)actControl).SetData(actProfile);
                actControl.Show();
                lblHeader.Text = ">> " + ((Controls.IUserControl)actControl).Header + " <<";
                pnlProfile.Visible = true;
            }
            //else
            //{
            //    if (t.GetInterface(typeof(Client.Controls.IUserControlNoProfile).Name) != null)
            //    {
            //        ((Client.Controls.IUserControlNoProfile)this.actControl).SetData();
            //        this.actControl.Show();
            //        this.lblHeader.Text = ">> " + ((Client.Controls.IUserControlNoProfile)this.actControl).Header + " <<";
            //        this.pnlProfile.Visible = false;
            //    }
            //}

            Cursor = Cursors.Default;
        }

        private void ProfileSaved()
        {
            Globals.SaveSettings();
            if (actProfile != null && actProfile.ProfileName != (string)cboProfiles.SelectedItem)
            {
                bSetData = true;
                FillProfiles();
                cboProfiles.SelectedItem = actProfile.ProfileName;
                bSetData = false;
            }
        }

        private void ProfileDeleted()
        {
            bSetData = true;

            Globals.AppSettings.DeleteProfile(actProfile);
            actProfile = null;

            FillProfiles();
            if (Globals.AppSettings.Profiles.Count == 0)
                actProfile = null;
            else
            {
                actProfile = Globals.AppSettings.Profiles[0];
                cboProfiles.SelectedItem = actProfile.ProfileName;
            }
            bSetData = false;
            SetProfile();
        }

        private void NewProfileAdded()
        {
            bSetData = true;
            actProfile = Globals.AppSettings.AddNewProfile();
            FillProfiles();
            cboProfiles.SelectedItem = actProfile.ProfileName;
            bSetData = false;
            SetProfile();
        }

        private void TsbUserControlClick(object sender, EventArgs e)
        {
            ShowUserControl((UserControl)Activator.CreateInstance(((Controls.ControlInfo)((ToolStripButton)sender).Tag).Type));
        }

        private void TsbAboutClick(object sender, EventArgs e)
        {
            ShowUserControl(new Controls.UcAbout());
        }

        private void TsbExitClick(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void SetProfile()
        {
            actProfile = Globals.AppSettings.Profiles.Count == 0
                             ? null
                             : Globals.AppSettings.Profiles[cboProfiles.SelectedIndex];

            if (actControl != null)
                ((Controls.IUserControl)actControl).SetData(actProfile);
        }

        private void CboProfilesSelectedIndexChanged(object sender, EventArgs e)
        {
            if (!bSetData)
                SetProfile();
        }

    }
}